package com.j.lemon.learn.basic.utils;

/**
 * @Author lijunjun
 * @Date 2018-12-20 18:16
 * @Description
 */



import java.security.MessageDigest;

public class MD5Utils {
    public MD5Utils() {
    }

    public static String encrypt32(String encryptStr) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(encryptStr.getBytes());
            StringBuffer hexValue = new StringBuffer();

            for(int i = 0; i < md5Bytes.length; ++i) {
                int val = md5Bytes[i] & 255;
                if (val < 16) {
                    hexValue.append("0");
                }

                hexValue.append(Integer.toHexString(val));
            }

            encryptStr = hexValue.toString();
            return encryptStr;
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    public static String encrypt16(String encryptStr) {
        return encrypt32(encryptStr).substring(8, 24);
    }

    public static String getMD5(String message) {
        String md5 = "";

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageByte = message.getBytes("UTF-8");
            byte[] md5Byte = md.digest(messageByte);
            md5 = bytesToHex(md5Byte);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return md5;
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuffer hexStr = new StringBuffer();

        for(int i = 0; i < bytes.length; ++i) {
            int num = bytes[i];
            if (num < 0) {
                num += 256;
            }

            if (num < 16) {
                hexStr.append("0");
            }

            hexStr.append(Integer.toHexString(num));
        }

        return hexStr.toString().toUpperCase();
    }
}

